<?php

namespace App\Controller\Auth\Admin;

use App\Controller\AbstractController;
use App\Service\Auth\AdminAuthService;
use App\Validator\Auth\Admin\LoginValidator;
use Hyperf\Di\Annotation\Inject;

class LoginController extends AbstractController
{

    #[Inject]
    protected LoginValidator $loginValidator;

    #[Inject]
    protected AdminAuthService $adminAuthService;

    // 账密登录
    public function pwd()
    {
        $params = $this->allParams();
        $va = $this->loginValidator->pwdLogin($params);
        [$r, $data] = $this->adminAuthService->pwdLogin($va);
        if ($r){
            return $this->success($data,'登录成功');
        }
        return $this->fail($data);
    }

    public function refreshToken()
    {
        $params = $this->allParams();
        $va = $this->loginValidator->refreshToken($params);
        [$r, $data] = $this->adminAuthService->refreshToken($va['refresh_token']);
        if ($r){
            return $this->success($data,'登录成功');
        }
        return $this->fail($data);
    }

    public function out()
    {
        $adminId = $this->getAdminId();
        if (!$adminId){
            return $this->success(null,'退出成功');
        }
        $this->adminAuthService->logout($adminId);
        return $this->success(null,'退出成功');
    }
}